import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user'
import cptt from './modules/cptt'
import ht from './modules/ht'
Vue.use(Vuex)

import { getAdver } from '@/api/elseApis.js'
export default new Vuex.Store({
  modules: {
    user,cptt,ht
  },
  state:{
    advertising:[]
  },
  mutations:{
    SET_ADVER:(state,data)=>{ state.advertising = data }
  },
  actions: {
    GET_ADVER:async ({commit,}) => {
      let { adver } = await getAdver().catch(e=>{
        console.error(e)
        return {adver:[false]}
      })
      if(adver[0] && adver.length <4 )
        adver.push([true])
      console.log({adver})
      commit('SET_ADVER',adver)
    }
  },
  getters:{
	  advertising:state=>state.advertising,
  }
})
/* state: {
	login: {
		data: "",
		isOk: false,
		user: {
			// uAccount: "",
			// uId: "",
			// uName: "(测试使用-跳过登录)",
			// uPassword: "",
			// uType: "1"
		}
	},
	cptts: [//比赛信息
		],
	// hisevents:[//历史赛事
	// 	],
	info: "柯柯对你说:\"你好!\""
  },
  mutations: {
	  getCPTT: state => {
	  	axios.get('./api/getCptts')
			.then(data=>{
				let result = data.data.data
				result.forEach(i=>{
					i.isGoing = (new Date(i.goingtime) - new Date())>0? false: true;
					i.time = new Date(i.overtime) - new Date()
					i.state = i.time>0? true: false;
					// i.url = require(i.url)
				})
				state.cptts = result
				// console.log(result)
			})
			.catch(err=>{
				console.log((err+"").split(":")[1])
			})
	  },
	  sortCptt(e){
		  
	  },
	 //  getHisEvent:state=>{
		// let url = '/api/getHisEvents?pageInfo='+state.hisevents.length+',4'
		// axios.get(url)
		// 	.then(data=>{
		// 		let id = state.login.user.uId
		// 		console.log(data)
		// 		data.data.forEach(i=>{
		// 			i.state = i.participantsId.includes(id)
		// 				this.hisevents.push(i)
		// 		})
		// 	})
		// 	.catch(err=>{
		// 		console.log((err+"").split(":")[1])
		// 	})
	 //  },
	  getInfo: e => {
	  	axios.get('https://v1.hitokoto.cn')
	  		 .then(a=>{e.info = a.data.hitokoto + "    —— " +  a.data.from})
	  		 .catch(a=>{e.info = "有什么东西丢失了"})
	  },
	  getLoginInfo: (state,data) => {
		state.login = data
	  },
	  logOut:(state)=>{//退出登陆
		  state.login = {
			  data:"",
			  isOk:false,
			  user:{
			  	// uAccount: "",
			  	// uId: "",
			  	// uName: "未登录",
			  	// uPassword: "",
			  	// uType: "0"
			  }
		  }
		  window.localStorage.removeItem("src_login");
	  },
	  setCptt:(state,data)=>{
		   console.log("data=",data)
		   const index = data[0]
		   const key = data[1].key
		   let newData = data[1].newDate
		   if(key == "isUse"){
			   if(newData == "0")
			   {
				   state.cptts.splice(index,1)
				   return;
			   }
			   if(newData == "1")
			   {
				   const cptt = data[1].cptt
				   state.cptts.push(cptt)
			   }
		   }
		   if(key == "overtime" || key == "goingtime"){
			newData = new Date(newData)
			//其实没必要
			if(key === "overtime")
				state.cptts[index].state = newData - new Date()>0? true: false;
			else
				state.cptts[index].isGoing = newData - new Date()>0? false: true;
			//setShow里做了
			const y = newData.getFullYear()
			const M = newData.getMonth() + 1
			const d = newData.getDate()
			const time = newData.toString().slice(16,24)
			newData = y+"-"+(M<10? "0"+M:M)+"-"+(d<10? "0"+d:d)+" "+time
		   }
		   console.log(newData)
		   state.cptts[index][key] = newData
		   
	  }
  }, */